package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


import entity.studentInfo;
import util.DBHelper;

//学生的业务逻辑类
public class studentInfoDAO
{
	//获取所有的学生信息
	public ArrayList<studentInfo> getAllStudent()
	{
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		ArrayList<studentInfo> list = new ArrayList<studentInfo>();
		
		try
		{
			conn = DBHelper.getConnection();
			String sql = "select * from studentInfo";
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			while(rs.next())
			{
				//System.out.println("=================" + rs.getString("name") + "==================");
				
				studentInfo stu = new studentInfo();
				stu.setId(rs.getInt("id"));
				stu.setName(rs.getString("name"));
				stu.setNumber(rs.getString("number"));
				stu.setGender(rs.getString("gender"));
				stu.setBirthday(rs.getDate("birthday"));
				stu.setHobby(rs.getString("hobby"));
				stu.setNative_place(rs.getString("native_place"));
				stu.setRemark(rs.getString("remark"));
				stu.setPicture(rs.getString("picture"));
				
				//学生信息加入list
				list.add(stu);
			}
			return list;
		} 
		catch (Exception e)
		{
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
		//释放资源，从小到大释放，conn 不能释放
		finally
		{
			if(rs != null)
			{
				try
				{
					rs.close();
				} catch (SQLException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				rs = null;
			}
			
			if(stmt != null)
			{
				try
				{
					stmt.close();
				} catch (SQLException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				stmt = null;
			}
		}
	}
}
